var path ;
function loading(url){
		path=url;
		$('#dg').datagrid({    
		    url:path+"/systemRole/findList",
		    checkOnSelect: false,
			selectOnCheck: false,
			singleSelect: false,
			nowrap: true, 
		    pagination:true,
		    fitColumns:true,
		    singleSelect:true,
		    rownumbers: true, //显示行号
			showfooter: true, //显示行尾
		    sortName: 'id',	
			sortOrder: 'desc',
			toolbar:'#datagrid_toolbar',
		    columns:[[
				{field:'id', title :'id', width : 50, checkbox: true},
				{field:'facName',title:'厂商名称',width:100,align : 'center',sortable:true},  
		        {field:'roleNo',title:'角色编码',width:100,align : 'center',sortable:true},    
		        {field:'roleName',title:'角色名',width:100,align : 'center',sortable:true},
		        {field:'type',title:'角色类型',width:100, align : 'center',sortable:true,
		        	formatter:function(value,row,index){
		        		if (1==value) return '厂商平台';
		        		else if (0==value) return '系统管理平台';
		        		else return '';
		        	}
		        },
		        {field:'status',title:'角色启用状态',width:100,align : 'center',sortable:true,
		        	formatter:function(value,row,index){
		        		if (-1==value) return '未启用';
		        		else if (1==value) return '已启用';
		        		else return '';
		        	}
		        },
		        {field:'isDeleted',title:'逻辑删除状态',width:100,align : 'center',sortable:true,
		        	formatter:function(value,row,index){
		        		if (-1==value) return '已删除';
		        		else if (1==value) return '未删除';
		        		else return '';
		        	}
		        },
		        {field:'remark',title:'备  注',width:100,align : 'center',sortable:true},
		        {field:'ids',title:'操作',width:60,align : 'center',
		        	formatter:function(value,row,index){
		        		 var a = '<a href="javascript:void(0)" class="easyui-linkbutton"  onclick="menuSet('+row.id+','+row.type+')">菜单分配</a>';
		        		 var b = '<a href="javascript:void(0)" class="easyui-linkbutton"  onclick="remove(this)">删除</a>';
		        		 return a+"&nbsp;&nbsp;"+b;
		        	}
		        }
		    ]]    
		});
		$('#cc,#sfac,#sfac2').combogrid({
			pageNumber:1,
			pageSize:5,
			pageList:[5,10,20,50,100],
		    panelWidth:440,
		    panelHeight:'',
		    fitColumns:true,
		    value:'',    
		    pagination:true,
		    idField:'id',    
		    textField:'facName',
		    rownumbers: true, //显示行号
			showfooter: true, //显示行尾
		    url:path+'/factoryCreate/selectPage',
		    columns:[[       
		        {field:'facName',title:'厂商名称',width:170,align : 'center',sortable:true},
		        {field:'facNo',title:'厂商编码',width:170,align : 'center',sortable:true} 
		    ]]    
		});
		
		$('#stype').combobox({
			onChange:function(rec){
				var temp = $('#stype').combobox("getValue");
				if(temp==0){
					$('#sfac').combobox('disable');
				}else{
					$('#sfac').combobox('enable');
				} 
			}
		});
};
	function menuSet(id,type){
		$('#win3').window('open');
		$('#tree').tree({
			lines:true,
			checkbox:true,
		    url:path+"/systemRole/findMenuByRoleId",
		    queryParams:{id:id,classify:type},
		    loadFilter: function(data){    
		        if (data.success){
		        	if(data.data.b==null){
		        		return data.data.a; 
		        	}else{
		        		return data.data.a.concat(data.data.b);
		        	}
		        } else {    
		            return null;    
		        }    
		    }
		});
	}
	function menuChange(){
		var menus=$('#tree').tree("getChecked");
		var ids='';
		$.each(menus,function(i,item){
			ids=ids+","+item.id;
		});
		$.ajax({
			url:path+"/systemRole/updateMenuRole",
			type:"post",
			data:{menuIds:ids,id:$('#dg').datagrid("getSelected").id},
			dataType:"json",
			success:function(data){
		        if(data.success){
					swal({
						title : '',
						text : data.message,
						icon : 'success',
						timer : 1500,
						buttons : false,
		        	})
					$('#win3').dialog("close");
		        }else{
		        	swal({
						title : '',
						text : data.message,
						icon : 'warning',
						timer : 1500,
						buttons : false,
		        	})
		        }  
			}
		});
	}
	function remove(rec){
		var a=getRowIndex(rec);
		var line = $('#dg').datagrid("getRows")[a];
		console.info(rec);
		$.ajax({
			url:path+"/systemRole/deletes?ids="+line.id,
			type:"post",
			dataType:"json",
			success:function(data){
		        if(data.success){
					swal({
						title : '',
						text : data.message,
						icon : 'success',
						timer : 1500,
						buttons : false,
		        	})
		        	$('#dg').datagrid("reload");
		        }else{
		        	swal({
						title : '',
						text : data.message,
						icon : 'warning',
						timer : 1500,
						buttons : false,
		        	})
		        }  
			}
		});
	}
	
	function removes(){
		var checks= $('#dg').datagrid("getChecked");
		var ids =[];
		$.each(checks,function(index,item){
			ids.push(item.id)
		})
 		$.ajax({
			url:path+"/systemRole/deletes?ids="+ids,
			type:"post",
			dataType:"json",
			success:function(data){
		        if(data.success){
					swal({
						title : '',
						text : data.message,
						icon : 'success',
						timer : 1500,
						buttons : false,
		        	})
		        	$('#dg').datagrid("reload");
		        }else{
		        	swal({
						title : '',
						text : data.message,
						icon : 'warning',
						timer : 1500,
						buttons : false,
		        	})
		        }  
			}
		});
	}
	
	function doSearch(){
		$('#dg').datagrid('load',{
			filterText: $('#itemid').val(),
			status: $('#st').combobox("getValue"),
			type:$('#ty').combobox("getValue"),
			facId:$('#cc').combobox("getValue")
		});
	}
	function add(){
		$("#win").window('open');
	}
	
	function closeWin(){
		$("#win").window('close');
		$("#win2").window('close');
		$("#win3").dialog('close');
	}
	function find(){
		var line = $('#dg').datagrid("getSelected");
		if(line==null){
			swal({
				title : '',
				text : "请选择相应记录",
				icon : 'warning',
				timer : 1500,
				buttons : false,
        	})
		}else{
			$('#update').form('load',line);
			$("#win2").window('open');
		}
	}
	function submit(){
		$('#ff').form('submit', {    
		    url:path+"/systemRole/insert",    
		    onSubmit: function(){    
		        // do some check    
		        // return false to prevent submit;    
		    },    
		    success:function(data){
		    	var json = JSON.parse(data);
		        if(json.success){
		        	swal({
						title : '',
						text : json.message,
						icon : 'success',
						timer : 1500,
						buttons : false,
		        	})
		        	$("#win").window('close');
		        	$("#ff").form("clear");
		        	$('#dg').datagrid("reload");
		        }else{
		        	swal({
						title : '',
						text : json.message,
						icon : 'warning',
						timer : 1500,
						buttons : false,
		        	})
		        }   
		    }    
		});
	}
	function update(){
		$('#update').form('submit', {    
		    url:path+"/systemRole/update",    
		    onSubmit: function(){    
		        // do some check    
		        // return false to prevent submit;    
		    },    
		    success:function(data){
		    	var json = JSON.parse(data);
		        if(json.success){
		        	swal({
						title : '',
						text : json.message,
						icon : 'success',
						timer : 1500,
						buttons : false,
		        	})
		        	$("#win2").window('close');
		        	$("#update").form("clear");
		        	$('#dg').datagrid("reload");
		        }else{
		        	swal({
						title : '',
						text : json.message,
						icon : 'warning',
						timer : 1500,
						buttons : false,
		        	})
		        }   
		    }    
		});
}